package timbap.visualizer.api.plugin.container;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import timbap.visualizer.api.context.PluginContext;
import timbap.visualizer.api.plugin.AbstractPlugin;
import timbap.visualizer.api.plugin.Plugin;

import java.util.ArrayList;
import java.util.List;

/**
 * The <code>AbstractPluginContainer</code> provides a common base
 * implementation of the <code>PluginContainer</code> interface
 * that should be used by all timbap plugins containing other
 * timbap plugins.
 *
 * @author  Andreas Pabst
 * @see     timbap.visualizer.api.plugin.container.PluginContainer
 * @see     timbap.visualizer.api.plugin.AbstractPlugin
 */
public abstract class AbstractPluginContainer extends AbstractPlugin implements PluginContainer {

    private static final Log LOG = LogFactory.getLog(AbstractPluginContainer.class);

    private List<Plugin> plugins = new ArrayList<Plugin>();

    @Override
    public void init(PluginContext pPluginContext) {
        super.init(pPluginContext);
        for (Plugin tPlugin: getPlugins()) {
            tPlugin.init(pPluginContext);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("All plugins of plugin container " + this + " initialized.");
        }
    }

    public void initPlugin(Plugin pPlugin) {
    	pPlugin.init(getContext());
    }

    public List<Plugin> getPlugins() {
		return plugins;
	}

	public void addPlugin(Plugin pPlugin) {
        LOG.debug("Adding sub-plugin " + pPlugin.getClass().getSimpleName());
        plugins.add(pPlugin);
    }

    public void removePlugin(Plugin pPlugin) {
        plugins.remove(pPlugin);
    }

}